﻿using ConfExpert.Datasets;
using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevTeam;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AddKons : XPopupForm
    {
        public AddKons()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void AddKons_Load(object sender, EventArgs e)
        {

            bs = new BindingSource(AppSession.Lookups, "Konstruksiyon");
            InitGridView(gv1, bs);
            gv1.Columns["Name"].BestFit();

            gleKompozisyon.Properties.Assign(RI(Rp.Kompozisyon));

            gleWeave.Properties.Assign(RI(Rp.Weave));
            gleYcWarp.Properties.Assign(RI(Rp.YarnCount));
            gleYcWarpE.Properties.Assign(RI(Rp.YarnCount));
            gleYcWarpS.Properties.Assign(RI(Rp.SpecialYarn));
            gleYcWeft.Properties.Assign(RI(Rp.YarnCount));
            gleYcWeftE.Properties.Assign(RI(Rp.YarnCount));
            gleYcWeftS.Properties.Assign(RI(Rp.SpecialYarn));
            //gleGroupESP.Properties.Assign(RI(Rp.GrupESP));
            //gleWidth.Properties.Assign(RI(Rp.EnGrubu));

            gleWeave.EditValueChanged += new EventHandler(gleWeave_EditValueChanged);
            gleYcWarp.EditValueChanged += new EventHandler(gleYcWarp_EditValueChanged);
            gleYcWarpE.EditValueChanged += new EventHandler(gleYcWarpWe_EditValueChanged);
            gleYcWarpS.EditValueChanged += new EventHandler(gleYcWarpS_EditValueChanged);
            gleYcWeft.EditValueChanged += new EventHandler(gleYcWeft_EditValueChanged);
            gleYcWeftE.EditValueChanged += new EventHandler(gleYcWeftWe_EditValueChanged);
            gleYcWeftS.EditValueChanged += new EventHandler(gleYcWeftS_EditValueChanged);
            //gleWidth.EditValueChanged += new EventHandler(gleWidth_EditValueChanged);
            //gleGroupESP.EditValueChanged += new EventHandler(gleGroupESP_EditValueChanged);
            //teQuality.EditValueChanged += new EventHandler(teQuality_EditValueChanged);

            teDensityIn1.EditValueChanged += new EventHandler(DensityInch1_EditValueChanged);
            teDensityIn2.EditValueChanged += new EventHandler(DensityInch1_EditValueChanged);

            foreach (string str in "KonstruksiyonId;YcWarp;YcWarpE;YcWarpS;YcWeft;YcWeftE;YcWeftS;Weave;D1;D2".Split(';'))
                gv1.Columns[str].Visible = false;

            gv1.Columns["Name"].Caption = "Konstruksiyon";

            //gv1.Columns["YcWarp"].ColumnEdit = RI(Rp.YarnCount);
            //gv1.Columns["YcWarpE"].ColumnEdit = RI(Rp.YarnCount);
            //gv1.Columns["YcWarpS"].ColumnEdit = RI(Rp.SpecialYarn);
            //gv1.Columns["YcWeft"].ColumnEdit = RI(Rp.YarnCount);
            //gv1.Columns["YcWeftE"].ColumnEdit = RI(Rp.YarnCount);
            //gv1.Columns["YcWeftS"].ColumnEdit = RI(Rp.SpecialYarn);
            //gv1.Columns["Weave"].ColumnEdit = RI(Rp.Weave);

            btnAdd.ItemClick += btnAdd_ItemClick;
            btnSave.ItemClick += btnSave_ItemClick;
            this.FormClosing += new FormClosingEventHandler(AddKons_FormClosing);
            btnClear.ItemClick += btnClear_ItemClick;
            gv1.DoubleClick += new EventHandler(gv1_DoubleClick);

            gv1.OptionsBehavior.Editable = false;

            btnSave.Enabled = false;
        }

        void btnAdd_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kaydet(false);
        }

        void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Kaydet(true);
        }

        void btnClear_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gleYcWarp.EditValue =
           gleYcWarpE.EditValue =
           gleYcWarpS.EditValue =
           gleYcWeft.EditValue =
           gleYcWeftE.EditValue =
           gleYcWeftS.EditValue =
           teDensityIn1.EditValue =
           teDensityIn2.EditValue =
           gleWeave.EditValue = null;
        }

        void teQuality_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleGroupESP_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleWidth_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gv1_DoubleClick(object sender, EventArgs e)
        {
            if (intParse(gv1.GetFocusedRowCellValue("KonstruksiyonId")) > 0)
            {
                SetFields(intParse(gv1.GetFocusedRowCellValue("KonstruksiyonId")));
                KonstruksiyonId = intParse(gv1.GetFocusedRowCellValue("KonstruksiyonId"));
                btnSave.Enabled = true;
            }
        }


        void AddKons_FormClosing(object sender, FormClosingEventArgs e)
        {
            bs.RemoveFilter();
        }

        void DensityInch1_EditValueChanged(object sender, EventArgs e)
        {
            if (!suspend)
            {
                suspend = true;
                teDensityIn1.BackColor = teDensityIn2.BackColor = Color.LightYellow;

                RefreshExp();
                suspend = false;
            }
        }

        bool suspend = false;

        private void SetFields(int KonstId)
        {
            MyData.KonstruksiyonRow dr = AppSession.Lookups.Konstruksiyon.FindByKonstruksiyonId(KonstId);
            if (!dr.IsYcWarpNull())
                gleYcWarp.EditValue = dr.YcWarp;
            if (!dr.IsYcWarpENull())
                gleYcWarpE.EditValue = dr.YcWarpE;
            if (!dr.IsYcWarpSNull())
                gleYcWarpS.EditValue = dr.YcWarpS;
            if (!dr.IsYcWeftNull())
                gleYcWeft.EditValue = dr.YcWeft;
            if (!dr.IsYcWeftENull())
                gleYcWeftE.EditValue = dr.YcWeftE;
            if (!dr.IsYcWeftSNull())
                gleYcWeftS.EditValue = dr.YcWeftS;
            if (!dr.IsD1Null())
                teDensityIn1.EditValue = dr.D1;
            if (!dr.IsD2Null())
                teDensityIn2.EditValue = dr.D2;
            if (!dr.IsWeaveNull())
                gleWeave.EditValue = dr.Weave;

        }

        void gleYcWeftS_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleYcWarpS_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleYcWeftWe_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleYcWeft_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleYcWarpWe_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleYcWarp_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        void gleWeave_EditValueChanged(object sender, EventArgs e)
        {
            RefreshExp();
        }

        string GetTerm(DataRow dr)
        {
            if (dr["Expression"] != null)
                if (dr["Expression"] != DBNull.Value)
                    if (dr["Expression"].ToString() != string.Empty)
                        return dr["Expression"].ToString();

            return dr["Name"].ToString();
        }

        void RefreshExp()
        { 
            string YcWarp = (gleYcWarp.EditValue != null   ? GetTerm(AppSession.Lookups.YarnCount.FindByYarnCountId(gleYcWarp.EditValue.ToInt())) : string.Empty);
            string YcWarpE = (gleYcWarpE.EditValue != null ? GetTerm(AppSession.Lookups.YarnCount.FindByYarnCountId(gleYcWarpE.EditValue.ToInt())) : string.Empty);
            string YcWarpS = (gleYcWarpS.EditValue != null ? GetTerm(AppSession.Lookups.SpecialYarn.FindBySpecialYarnId(gleYcWarpS.EditValue.ToInt())) : string.Empty);
            string YcWa = YcWarp +
                (YcWarpE != string.Empty && YcWarp != string.Empty ? " + " : string.Empty) + YcWarpE +
                (YcWarpS != string.Empty && (YcWarp != string.Empty || YcWarpE != string.Empty) ? " " : string.Empty) + YcWarpS;

            string YcWeft = (gleYcWeft.EditValue != null ? GetTerm(AppSession.Lookups.YarnCount.FindByYarnCountId(gleYcWeft.EditValue.ToInt())) : string.Empty);
            string YcWeftE = (gleYcWeftE.EditValue != null ? GetTerm(AppSession.Lookups.YarnCount.FindByYarnCountId(gleYcWeftE.EditValue.ToInt())) : string.Empty);
            string YcWeftS = (gleYcWeftS.EditValue != null ? GetTerm(AppSession.Lookups.SpecialYarn.FindBySpecialYarnId(gleYcWeftS.EditValue.ToInt())) : string.Empty);

            string YcWe = YcWeft +
                (YcWeftE != string.Empty && YcWeft != string.Empty ? " + " : string.Empty) + YcWeftE +
                (YcWeftS != string.Empty && (YcWeft != string.Empty || YcWeftE != string.Empty) ? " " : string.Empty) + YcWeftS;

            string Yc = YcWa + (YcWa != string.Empty && YcWe != string.Empty ? " x " : string.Empty) + YcWe;

            string D1 = teDensityIn1.Text.Replace(",", string.Empty);
            lblUnit.Text = intParse(D1) < 100 ? "CM." : "INCH";
            string D2 = teDensityIn2.Text.Replace(",", string.Empty);
            string D = D1 + (D1 != string.Empty && D2 != string.Empty ? "*" : string.Empty) + D2;
            string Dx = string.Empty;
            if (D != "0*0")
                Dx = D != string.Empty ? " (" + D + ")" : string.Empty;

            string Weave = (gleWeave.EditValue != null ? " " + GetTerm(AppSession.Lookups.Weave.FindByWeaveId(intParse(gleWeave.EditValue))) : string.Empty);

            lblPreview.Text = Yc + Dx + Weave;

            List<string> f = new List<string>();
            if (gleYcWarp.EditValue != null)
                f.Add("YcWarp = " + intParse(gleYcWarp.EditValue));
            if (gleYcWarpE.EditValue != null)
                f.Add("YcWarpE = " + intParse(gleYcWarpE.EditValue));
            if (gleYcWarpS.EditValue != null)
                f.Add("YcWarpS = " + intParse(gleYcWarpS.EditValue));
            if (gleYcWeft.EditValue != null)
                f.Add("YcWeft = " + intParse(gleYcWeft.EditValue));
            if (gleYcWeftE.EditValue != null)
                f.Add("YcWeftE = " + intParse(gleYcWeftE.EditValue));
            if (gleYcWeftS.EditValue != null)
                f.Add("YcWeftS = " + intParse(gleYcWeftS.EditValue));

            if (intParse(teDensityIn1.EditValue) > 0)
                f.Add("D1 = " + intParse(teDensityIn1.EditValue));
            if (intParse(teDensityIn2.EditValue) > 0)
                f.Add("D2 = " + intParse(teDensityIn2.EditValue));

            if (intParse(gleWeave.EditValue) > 0)
                f.Add("Weave = " + intParse(gleWeave.EditValue));

            bs.Filter = (f.Count > 0 ? string.Join(" AND ", f.ToArray()) : null);
        }


        private void Kaydet(bool SaveExisting)
        {
            if (intParse(gleYcWarp.EditValue) == 0 ||
                intParse(gleYcWeft.EditValue) == 0 ||
                intParse(teDensityIn1.EditValue) == 0 ||
                intParse(teDensityIn2.EditValue) == 0 ||
                intParse(gleWeave.EditValue) == 0 ||
                intParse(gleKompozisyon.EditValue) == 0
                )
                XtraMessageBox.Show("Zorunlu alanları doldurduktan sonra tekrar deneyin.");


            //if (intParse(ds.Hammadde.Compute("Sum(Yuzdesi)", string.Empty)) != 100)
            //{
            //    XtraMessageBox.Show("Bileşenlerin toplamı %100 olmalıdır.");
            //    return;
            //}
            else if (lblPreview.Text != string.Empty)
            {
                ds.Konstruksiyon.Clear();
                GetDetails("Konstruksiyon", "UpdKonstruksiyon",
                    SaveExisting ? KonstruksiyonId : 0,
                    lblPreview.Text,
                    intParse(gleKompozisyon.EditValue),
                    intParse(gleYcWarp.EditValue),
                    intParse(gleYcWarpE.EditValue),
                    intParse(gleYcWarpS.EditValue),
                    intParse(gleYcWeft.EditValue),
                    intParse(gleYcWeftE.EditValue),
                    intParse(gleYcWeftS.EditValue),
                    intParse(teDensityIn1.EditValue),
                    intParse(teDensityIn2.EditValue),
                    intParse(gleWeave.EditValue),
                    WhoAmI
                    );

                KonstruksiyonId = 0;
                if (ds.Konstruksiyon.Rows.Count > 0)
                    KonstruksiyonId = ds.Konstruksiyon[0].KonstruksiyonId;

                AppSession.MainForm.RefreshData();
                //if (((DevExpress.XtraEditors.BaseButton)sender).Name.Contains("Exit"))
                //    this.DialogResult = System.Windows.Forms.DialogResult.OK;
            }
        }

        int KonstruksiyonId = 0;
    }
}